dojo.provide("apt.Event");
dojo.require("apt.EventPopover");
dojo.require("dijit._WidgetBase");
dojo.require("dijit._TemplatedMixin");

dojo.declare("apt.Event", [dijit._WidgetBase, dijit._TemplatedMixin], { 
  templateString: "<div class=\"event\" style=\"overflow: hidden\"><span class=\"info\">${info}</span></div>",
  widgetsInTemplate: false,
  info: '',
  type: '',
  parent: '',
  
  constructor: function(data){
    this.inherited(arguments);
    dojo.safeMixin(this, data);
    if (this.type == 'reservation'){
      if (this.e.ReservationInfo.hasOwnProperty('Vehicle') && this.e.ReservationInfo.Vehicle.hasOwnProperty('LicenseNumber') && this.e.ReservationInfo.Vehicle.LicenseNumber.length > 0) {        
        this.info = this.e.ReservationInfo.Vehicle.LicenseNumber;
      }
      // this.info = this.info + "Start: " + data.e.StartTimeJSDate.getHours() + ":" + data.e.StartTimeJSDate.getMinutes();
      // this.info = this.info + "  End: " + data.e.EndTimeJSDate.getHours() + ":" + data.e.EndTimeJSDate.getMinutes();
    } else {
      this.info = '';
    }    
    // if (data.e.hasOwnProperty('StartHour')){
    //   this.info = data.e.StartHour + " " + data.e.EndHour;
    // }
    
    // this.info = this.info + "<br/> Duration: " + data.e.Duration;
  },
  
  postCreate: function(){
    if (this.type == "reservation"){
      var hue = parseInt(this.e.ReservationNumber.toString().split("").reverse().join("").substr(0,3));
      hue = hue%360;
      dojo.style(this.domNode, 'background', 'hsl(' + hue + ', 32%, 50%)');      
      dojo.style(this.domNode, 'color', 'hsl(' + hue + ', 32%, 80%)');      
      dojo.style(this.domNode, 'border-color', 'hsl(' + hue + ', 32%, 30%)');      
    }
  },
  
  showInfo: function(event){
    if (this.type == 'reservation'){
      var info = this.e.ReservationInfo;
            
      if (typeof document.popover == "object" ){
        document.popover.destroyRecursive();
      };
      document.popover = new apt.EventPopover({
          title: "Reservation Info",
          contact: '',
          license: info.Vehicle.LicenseNumber,
          reservationNumber: this.e.ReservationNumber,
          start: this.e.StartTimeJSDate,
          end: this.e.EndTimeJSDate
      });
      
      document.popover.show();
    }
  }
});
